package com.dingreading.cloud.soms.util;


import com.dingreading.cloud.common.util.WxGzhUtil;
import com.dingreading.cloud.soms.entity.Cmservices;
import com.dingreading.cloud.soms.entity.Members;
import com.dingreading.cloud.soms.entity.Relatives;
import com.dingreading.cloud.soms.service.MembersService;
import com.dingreading.cloud.soms.service.RelativesService;
import com.dingreading.cloud.soms.util.wxtemplate.TemplateMessUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class WxTemplateUtil {
    @Resource
    private RelativesService relativesService;
    @Resource
    private MembersService membersService;

    /**
     * 点名完成以后将剩余次数推送给家长公众号
     *
     * @param cmservicesList
     */
    @Async
    public void wxSendToRelatives(List<Cmservices> cmservicesList, String token, String beginTime) {
        if (CollectionUtils.isEmpty(cmservicesList)) {
            return;
        }
        List<String> memberUidsForPush = cmservicesList.stream()
                .map(Cmservices::getMemberUid)
                .distinct()
                .collect(Collectors.toList());

        Map<String, Integer> memberTotalLessonsMap = cmservicesList.stream()
                .collect(Collectors.groupingBy(
                        Cmservices::getMemberUid,
                        Collectors.summingInt(c -> c.getLeftLessons() == null ? 0 : c.getLeftLessons())
                ));

        Map<String, String> memberProductNameMap = cmservicesList.stream()
                .collect(Collectors.toMap(
                        Cmservices::getMemberUid,
                        Cmservices::getProductName,
                        (existingName, newName) -> existingName
                ));

        List<Members> membersServiceByUids = membersService.getByUids(memberUidsForPush);
        List<Long> memberIds = membersServiceByUids.stream().map(Members::getId).collect(Collectors.toList());
        Map<Long, Members> longMembersMap = membersServiceByUids.stream().collect(Collectors.toMap(Members::getId, Function.identity()));

        List<Relatives> relativesList = relativesService.getByMemberIdsAndGzhOpenIdAndGzhPush(memberIds, 1);
        if (CollectionUtils.isNotEmpty(relativesList)) {
            for (Relatives relatives : relativesList) {
                if (StringUtils.isBlank(relatives.getGzhOpenId()))
                    continue;
                Members members = longMembersMap.get(relatives.getMemberId());
                if (members == null) continue;
                Integer totalLeftLessons = memberTotalLessonsMap.get(members.getUid());
                String productInfo = memberProductNameMap.get(members.getUid());

                if (totalLeftLessons == null) continue;

                String leftLessonCntStr = String.valueOf(totalLeftLessons == 0 ? 0 : totalLeftLessons);

                String build = TemplateMessUtils.residueDegree(relatives.getGzhOpenId(), productInfo, beginTime, members.getFullName(), leftLessonCntStr);
                // 发送微信消息
                WxGzhUtil.sendTemplate(build, token);
            }
        }
    }


}
